<template>
  <div v-if="dialogVisible" class="modal">
    <div class="modal-content">
      <h3>新建文件</h3>

      <div class="input-group">
        <input
            v-model="newFileName"
            placeholder="输入文件名称"
            type="text"
            @keyup.enter="confirmCreateFile"
        >
        <select v-model="selectedFileType" class="file-type-select">
          <option value=".txt">.txt</option>
          <option value=".doc">.doc</option>
          <option value=".docx">.docx</option>
          <option value=".xlsx">.xlsx</option>
          <option value="*">所有类型</option>
        </select>
      </div>

      <div class="filename-preview">
        生成文件名: <span class="preview-text">{{ generatedFileName }}</span>
      </div>

      <div class="modal-actions">
        <button class="cancel-btn" @click="cancelCreateFile">取消</button>
        <button class="confirm-btn" @click="confirmCreateFile">确定</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, computed, watch} from 'vue'

// 定义props和emit
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:dialogVisible', 'createFile'])

// 响应式数据
const newFileName = ref('')
const selectedFileType = ref('.txt')

// 计算生成的完整文件名
const generatedFileName = computed(() => {
  if (!newFileName.value) return '请输入文件名称'

  if (selectedFileType.value === '*') {
    return newFileName.value
  } else {
    return newFileName.value + selectedFileType.value
  }
})

// 确认创建文件
const confirmCreateFile = () => {
  if (!newFileName.value.trim()) {
    alert('文件名不能为空')
    return
  }

  emit('createFile', generatedFileName.value)
  closeDialog()
}

// 取消创建文件
const cancelCreateFile = () => {
  closeDialog()
}

// 关闭对话框
const closeDialog = () => {
  newFileName.value = ''
  selectedFileType.value = '.txt'
  emit('update:dialogVisible', false)
}

// 监听对话框显示状态
watch(() => props.dialogVisible, (newVal) => {
  if (newVal) {
    // 对话框显示时，清空之前的输入
    newFileName.value = ''
    selectedFileType.value = '.txt'
  }
})
</script>

<style scoped>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 5px 25px rgba(0, 0, 0, 0.2);
  width: 450px;
  max-width: 90%;
}

.modal-content h3 {
  margin-bottom: 20px;
  color: #2c3e50;
  font-size: 1.5rem;
  text-align: center;
}

.input-group {
  display: flex;
  margin-bottom: 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  overflow: hidden;
}

input {
  flex: 1;
  padding: 12px 15px;
  border: none;
  outline: none;
  font-size: 16px;
}

.file-type-select {
  padding: 0 15px;
  border: none;
  border-left: 1px solid #ddd;
  outline: none;
  background: #f8f9fa;
  cursor: pointer;
  font-size: 15px;
  width: 150px;
}

.filename-preview {
  margin-bottom: 20px;
  padding: 10px;
  background-color: #f8f9fa;
  border-radius: 6px;
  font-size: 14px;
  color: #495057;
}

.preview-text {
  color: #3b5bdb;
  font-weight: 500;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

button {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s ease;
}

.cancel-btn {
  background-color: #f1f3f5;
  color: #495057;
}

.cancel-btn:hover {
  background-color: #e9ecef;
}

.confirm-btn {
  background-color: #4dabf7;
  color: white;
}

.confirm-btn:hover {
  background-color: #339af0;
}
</style>